package com.quanquan.user.message;

import com.quanquan.user.entity.User;
import com.quanquan.user.exception.UserException;
import com.quanquan.user.service.UserService;
import com.quanquan.user.service.weixin.MiniProgramProxy;
import com.quanquan.user.service.weixin.Proxy;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Objects;

/**
 * 功能描述:处理会员积分变动发送消息提醒的队列
 *
 * @ClassName: MqSignInContinuousReceiver
 * @Author: xiaoqianh丶
 * @Date: 2019-04-05 23:02
 * @Version: V1.0
 */
@Slf4j
@Component
public class MqSendMessageReceiver {


    public static final String REGISTERED = "registered";
    public static final String PARKINGFEE = "parkingFee";
    public static final String INTEGRALCHANG = "integralChange";

    private org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    private final UserService userService;
    private final Proxy proxy;
    private final MiniProgramProxy miniProgramProxy;
    @Autowired
    public MqSendMessageReceiver(UserService userService, Proxy proxy, MiniProgramProxy miniProgramProxy) {
        this.userService = userService;
        this.proxy = proxy;
        this.miniProgramProxy = miniProgramProxy;
    }

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "sendMessage",
                    durable = "true"),
            exchange = @Exchange(value = "send.exchange",
                    type = "topic"),
            ignoreDeclarationExceptions = "true",
            key = "send.*")})
    @RabbitHandler
    public void checkSignContinuous(Message message, Channel channel) throws IOException, UserException {
        //会员id
        String userId = Objects.requireNonNull(message.getHeaders().get("userId")).toString();
        String type = Objects.requireNonNull(message.getHeaders().get("type")).toString();
        String plateNum = Objects.requireNonNull(message.getHeaders().get("plateNum")).toString();
        String fee = Objects.requireNonNull(message.getHeaders().get("fee")).toString();
        String inTime = Objects.requireNonNull(message.getHeaders().get("inTime")).toString();
        String outTime = Objects.requireNonNull(message.getHeaders().get("outTime")).toString();
        String time = Objects.requireNonNull(message.getHeaders().get("time")).toString();
        String delIntegral = Objects.requireNonNull(message.getHeaders().get("delIntegral")).toString();
        String integral = Objects.requireNonNull(message.getHeaders().get("integral")).toString();
        String name = Objects.requireNonNull(message.getHeaders().get("name")).toString();

        //手工ack
        long deliveryTag = Long.parseLong(Objects.requireNonNull(message.getHeaders().get(AmqpHeaders.DELIVERY_TAG)).toString());
        channel.basicAck(deliveryTag, false);
        //根据用户id查询用户openid
        User user = userService.getUserById(userId);
        /*if (REGISTERED.equals(type)) {
            RegSuccessRequest regSuccessRequest = new RegSuccessRequest();
            regSuccessRequest.setOpenId(user.getWxOpenid());
            regSuccessRequest.setMemberName(user.getMemberName());
            regSuccessRequest.setPhone(user.getPhone());
            regSuccessRequest.setDateTime(user.getCreateTime());
            miniProgramProxy.sendRegSuccessMessage(regSuccessRequest);
        }
        if (PARKINGFEE.equals(type)) {
            ParkingFeeRequest request = new ParkingFeeRequest();
            request.setOpenId(user.getWxOpenid());
            request.setPlateNum(plateNum);
            request.setFee(fee);
            request.setInTime(inTime);
            request.setOutTime(outTime);
            request.setTime(time);
            miniProgramProxy.parkingFee(request);
        }
        if (INTEGRALCHANG.equals(type)){
            IntegralExchangeRequest templateRequest = new IntegralExchangeRequest();
            templateRequest.setOpenId(user.getWxOpenid());
            templateRequest.setMemberName(user.getMemberName());
            templateRequest.setDelIntegral(delIntegral);
            templateRequest.setIntegral(user.getIntegral()+" 积分");
            templateRequest.setTime(time);
            templateRequest.setName(name);
            templateRequest.setPhone(user.getPhone());
            miniProgramProxy.integralExchange(templateRequest);
        }*/

    }
}